--[[
    商店管理
]]
local shopManager = BaseClass("shopManager")

function shopManager : __init(shopPanel)
    -- 查找组件
    self.tran = shopPanel
    -- local goodConfig = require("GoodConfig").New()
    local goodConfigs = Global.GoodsDate
    local content = self.tran.transform : Find("Scroll View/Viewport/Content")
    local btnBuy = self.tran.transform : Find("BtnBuy")

    -- 初始化商品格子
    for i = 1, #goodConfigs, 1 do
        local shopItem = GameObject.Instantiate(Resources.Load("UIItem/ShopItem"), content.transform)
        require("ShopItem").New(shopItem, goodConfigs[i])
    end

    local btnBuyGoods = btnBuy : GetComponent(typeof(CS.UnityEngine.UI.Button))
    btnBuyGoods.onClick : AddListener(function()
        print("ShopManager输出1：购买物品")
        print("ShopManager输出2：购买物品名称".. Global.SelectItemDate.name)
        -- 商品数量减一
        Global.SelectItemDate.count = Global.SelectItemDate.count - 1
        -- 金币减少
        Global.CoinCount = Global.CoinCount - Global.SelectItemDate.price
        
        -- 刷新金币文本
        local textPrice = self.tran.transform:Find("TextPrice")
        textPrice = textPrice:GetComponent("Text")
        textPrice.text = "金币："..Global.CoinCount
        -- 调用刷新商品数量方法
        Global.SelectItemDate.shopItemInstance : RefreshCountText(Global.SelectItemDate.count)
        -- 存入数据缓存区中
        require("DateBuffer").New(Global.DateBuffers, Global.SelectItemDate)
        print("ShopManager输出3：数据缓存区数量：".. #Global.DateBuffers)

        -- 更新全局数据
        for _, item in ipairs(Global.GoodsDate) do
            if item.id == Global.SelectItemDate.id then
                item.count = Global.SelectItemDate.count
                break
            end
        end
    end)
end

return shopManager